/**
* webplayer common
* Author: Feil.Wang
* Date: 2013.07 
*/


$(function(){
	webplayer.init();
	$(window).resize(function(e){
		if(e.target.tagName == undefined){
			webplayer.resizeInit();
		}
	});

	//表情
	$(".phiz-group").hover(function(){
		$(this).addClass("phiz-group-cur");
		$(this).children("ul").show();
	},function(){
		$(this).removeClass("phiz-group-cur");
		$(this).children("ul").hide();
	});
	
	//选择聊天对象
	$(".select-box").click(function(){
		if($(".select-option").css("display")=="none"){
			$(".select-option").slideDown(function(){
				$(this).css("overflow","auto")
			});
		}else{
			$(".select-option").slideUp();
		}
	});
	$(".select-option li").click(function(e){
		var name = $(this).text();
		$("#selectedName").text(name);
		$(".select-option").slideUp();
		e.stopPropagation()
	});
	
	$(document.body).click(function(e){
		var target = $(e.target);
		if(target.closest(".select-box").length == 0){
			$(".select-option").slideUp();
		}
		if(target.closest(".shortcut-1").length == 0){
			$(".quick-layout-tips").animate({
				"width" : "0px"
			},300);
		}
	});
	
	//窗口拖拽
	$( ".main > div" ).draggable({
		containment: ".web",
		handle: ".comm-title",
		cursor: "move",
		stack: ".main > div"
	});
	//文字直播拖拽
	$( ".text-live-container" ).draggable({
		containment: ".web",
		handle: ".text-live-left",
		cursor: "move",
		stack: ".main > div"
	});
	
	//窗口大小调整

	$( ".ppt-container" ).resizable({
		containment: ".web",
		minWidth: 263,
		minHeight: 200,
		resize: function( event, ui ) {
			webplayer.pptInnerSize();
		}
	});
	$( ".qa-container" ).resizable({
		containment: ".web",
		minWidth: 263,
		minHeight: 200,
		resize: function( event, ui ) {
			webplayer.qaInnerSize();
		}
	});
	$( ".video-container" ).resizable({
		containment: ".web",
		minWidth: 263,
		minHeight: 200,
		resize: function( event, ui ) {
			webplayer.videoInnerSize();
		}
	});
	$( ".chat-container" ).resizable({
		containment: ".web",
		minWidth: 263,
		minHeight: 200,
		resize: function( event, ui ) {
			webplayer.chatInnerSize();
		}
	});
	$( ".outline-container" ).resizable({
		containment: ".web",
		minWidth: 263,
		minHeight: 200,
		resize: function( event, ui ) {
			webplayer.outlineInnerSize();
		}
	});
	
	//模拟播放、暂停
	$("#playBtn").click(function(){
		if($(this).hasClass("btn-play")){
			$(this).attr({
				"class" : "btn-pause",
				"title" : "暂停"
			});
		}else{
			$(this).attr({
				"class" : "btn-play",
				"title" : "播放"
			});
		}
	});
	
	//模拟关闭、开启视频
	$("#videoCtrlBtn").click(function(){
		if($(this).hasClass("btn-close-video")){
			$(this).attr({
				"class" : "btn-open-video",
				"title" : "打开视频"
			});
		}else{
			$(this).attr({
				"class" : "btn-close-video",
				"title" : "关闭视频"
			});
		}
	});
	
	//模拟静音
	$("#soundBtn").click(function(){
		if($(this).hasClass("horn-icon")){
			$(this).attr("class","horn-off-icon");
		}else{
			$(this).attr("class","horn-icon");
		}
	});
	
	//音量滑块
	$( "#volSlider" ).slider({
		value: 50,
		change: function() {
			var val = $( "#volSlider a" ).css("left");
			$( "#volSlider span" ).width(val);
			if(parseInt(val)==0){
				$("#soundBtn").attr("class","horn-off-icon");
			}else{
				$("#soundBtn").attr("class","horn-icon");
			}
		}
	});
	
	//进度条
	$( "#playerProgressBar").slider({
		value: 30,
		change: function() {
			var style = $( "#playerProgressBar a" ).attr("style").split(":")[1];
			var val = parseInt(style)+"%";
			$( ".progress-bar-elapsed" ).width(val);
		}
	});
	
	//文字直播选择语言
	$("#selectLang").click(function(){
		var list = $(".lang-list");
		if(list.css("display")=="none"){
			list.slideDown(200);
		}else{
			list.slideUp(200);
		}
	});
	
	//文字直播区域收缩
	$(".btn-textlive").click(function(){
		var txtLiveBox = $(".text-live-container");
		if(txtLiveBox.hasClass("box-isopen")){
			txtLiveBox.animate({
				width:'25px'
			},200);
			txtLiveBox.removeClass("box-isopen");
		}else{
			txtLiveBox.animate({
				width:'700px'
			},200);
			txtLiveBox.addClass("box-isopen");
		}
	});
	
	//在线用户
	$("#userList").click(function(){
		var userListDiv = $(".user-list-wrap");
		if(!$(this).hasClass("user-icon-open")){
			userListDiv.show();
			$(this).addClass("user-icon-open");
		}else{
			userListDiv.hide();
			$(this).removeClass("user-icon-open");
		}
	});
	
	//问答、留言切换
	$(".qa-tabs span").click(function(){
		var index = $(this).index();
		$(this).addClass("current").siblings().removeClass("current");
		$(".qa-main > div").eq(index).show().siblings().hide();
	});
	
	//展开PPT详细信息
	$(".btn-ppt-switch").click(function(){
		var pptInfo = $(".ppt-info");
		if(!pptInfo.hasClass("ppt-info-all")){
			pptInfo.animate({
				"height" : $(".ppt-container").height() - 46
			},300,function(){
				pptInfo.addClass("ppt-info-all");
				pptInfo.css("overflow","visible");
			});		
		}else{
			$(".ppt-info-inner").scrollTop(0);
			pptInfo.animate({
				height : 74
			},300,function(){
				pptInfo.removeClass("ppt-info-all");
				pptInfo.css("overflow","visible");
			});	
		}
	});
	
	$(".btn-switch").bind("click",function(){switchVideoToPPT(0)});
	
	//窗口最小化及还原
	WidgetMinimize(".ppt-container",".shortcut-2").minimize();
	WidgetMinimize(".video-container",".shortcut-3").minimize();
	WidgetMinimize(".chat-container",".shortcut-4").minimize();
	WidgetMinimize(".qa-container",".shortcut-5").minimize();
	WidgetMinimize(".outline-container",".shortcut-4").minimize();
	
});
//打开快速布局工具条
function openLayoutBar(){
	var $bar = $(".quick-layout-tips");
	if($bar.width()==0){
		$bar.animate({
			"width" : "240px"
		},300);
	}else if($bar.width()==240){
		$bar.animate({
			"width" : "0px"
		},300);
	}
}

//遮罩显示
function coverLayerShow(){
	$("#coverLayer").show().css("z-index","990");
}
//遮罩隐藏
function coverLayerHide(){
	$("#coverLayer").hide();
}

//显示点名窗口
function callName(){
	$(".callname-container").show().css("z-index","999");
	coverLayerShow();
}
//隐藏点名窗口
function callNameClose(){
	$(".callname-container").hide();
	coverLayerHide();
}
//显示抽奖窗口
function lotteryShow(){
	$(".lottery-container").show().css("z-index","999");
	lottery();//模拟抽奖
}
//隐藏抽奖窗口
function lotteryClose(){
	$(".lottery-container").hide();
}
//显示附件窗口
function accessoryShow(){
	$(".accessory-container").show().css("z-index","999");
}
//隐藏附件窗口
function accessoryClose(){
	$(".accessory-container").hide();
}
//显示投票调查窗口
function voteShow(){
	$(".vote-container").show().css("z-index","999");
	coverLayerShow();
}
//隐藏投票调查窗口
function voteClose(){
	$(".vote-container").hide();
	coverLayerHide();
}
//显示投票调结果查窗口
function voteResultShow(){
	$(".vote-result-container").show().css("z-index","999");
	coverLayerShow();
}
//隐藏投票调查结果窗口
function voteResultClose(){
	$(".vote-result-container").hide();
	coverLayerHide();
}
//显示优选网络
function chooseNetShow(){
	$(".choose-net-container").show();
}
//隐藏优选网络
function chooseNetClose(){
	$(".choose-net-container").hide();
}

//模拟抽奖
function lottery(){
	setTimeout(function(){
		$(".lottery-animate").hide();
		$("#lotteryOwner").show();
	},3000)
}

//底部按钮操作
function toggleWidgets(target){
	var $target = $(target);
	if($target.css("visibility") == "hidden"){
		$target.css("visibility","visible");
	}else{
		$target.css("visibility","hidden");
	}
}
//隐藏模块
function hiddenWidgets(target){
	var  $target = $(target);
	$target.css("visibility","hidden");
}

//视频全屏、还原操作
var currentVideoPos = {};//记录视频窗口当前位置
function videoScreenCtrl(){
	var $videoWindow = $(".video-container");	
	if(!$videoWindow.hasClass("v-full-screen")){
		$("html").css("overflow","hidden");
		var scrollLeftOffset = $("html").scrollLeft();//获得滚动条左偏移量
		var scrollTopOffset = $("html").scrollTop();//获得滚动条上偏移量
		currentVideoPos._width = $videoWindow.css("width");
		currentVideoPos._height = $videoWindow.css("height");
		currentVideoPos._left = $videoWindow.css("left");
		currentVideoPos._top = $videoWindow.css("top");
		$videoWindow.css({
			"width" : $(window).width(),
			"height" :  $(window).height() + 28,
			"left" : scrollLeftOffset,
			"top" : scrollTopOffset-28,
			"z-index" : 999
		});
		$videoWindow.find("object").css({
			"height" : "100%"
		});
		$videoWindow.addClass("v-full-screen");
		$( ".video-container" ).resizable("disable");
		$(".btn-fullscr").addClass("btn-fullsrc-exit");
		$(".btn-switch").unbind("click");
	}else{		
		$("html").css("overflow","auto");
		$videoWindow.css({
			"width" : currentVideoPos._width,
			"height" : currentVideoPos._height,
			"left" : currentVideoPos._left,
			"top" : currentVideoPos._top,
			"z-index" : "auto"
		});
		
		$videoWindow.removeClass("v-full-screen");
		$( ".video-container" ).resizable("enable");
		$(".btn-fullscr").removeClass("btn-fullsrc-exit");
		$(".btn-switch").bind("click",function(){switchVideoToPPT(0)});
	}
	webplayer.videoInnerSize();
}

//视频文档切换
var currentPptPos = {}; //记录文档窗口当前位置

function switchVideoToPPT(type){
	/**
	* type 有0,1,2三种状态
	* 0为自由切换
	* 1为文档至主窗口
	* 2为视频至主窗口
	*/
	
	var $videoWindow = $(".video-container");
	var $pptWindow = $(".ppt-container");
	var pptIsMain = $pptWindow.hasClass("ppt-is-main");
	
	currentVideoPos._width = $videoWindow.css("width");
	currentVideoPos._height = $videoWindow.css("height");
	currentVideoPos._left = $videoWindow.css("left");
	currentVideoPos._top = $videoWindow.css("top");

	currentPptPos._width = $pptWindow.css("width");
	currentPptPos._height = $pptWindow.css("height");
	currentPptPos._left = $pptWindow.css("left");
	currentPptPos._top = $pptWindow.css("top");


	if(type == 0){
		if(pptIsMain){
			switchFn();
			$pptWindow.removeClass("ppt-is-main");
		}else if(!pptIsMain){
			switchFn();
			$pptWindow.addClass("ppt-is-main");
		}
		
	}else if(type == 1){
		if(!pptIsMain){
			switchFn();
			$pptWindow.addClass("ppt-is-main");
		}
	}else if(type == 2){
		if(pptIsMain){
			switchFn();
			$pptWindow.removeClass("ppt-is-main");
		}
	}
	
	function switchFn(){
		$pptWindow.find("object,embed").css("visibility","hidden");
		$videoWindow.find("object,embed").css("visibility","hidden");
		$pptWindow.animate({
			"width" : currentVideoPos._width,
			"height" : currentVideoPos._height,
			"left" : currentVideoPos._left,
			"top" : currentVideoPos._top
			
		},300,function(){
			webplayer.pptInnerSize();
			setTimeout(function(){$pptWindow.find("object,embed").css("visibility","inherit")},300)
		});
		//video to main
		$videoWindow.animate({
			"width" : currentPptPos._width,
			"height" : currentPptPos._height,
			"left" : currentPptPos._left,
			"top" : currentPptPos._top
			
		},300,function(){
			webplayer.videoInnerSize();
			setTimeout(function(){$videoWindow.find("object,embed").css("visibility","inherit")},300)
		});
	}
}


//文档全屏与退出
function pptScreenCtrl(type){
	/**
	* type == 1 全屏
	* type == 0 退出全屏
	*/
	var $pptWindow = $(".ppt-container");
	if(type==1){
		$("html").css("overflow","hidden");
		var scrollLeftOffset = $("html").scrollLeft();//获得滚动条左偏移量
		var scrollTopOffset = $("html").scrollTop();//获得滚动条上偏移量
		currentPptPos._width = $pptWindow.css("width");
		currentPptPos._height = $pptWindow.css("height");
		currentPptPos._left = $pptWindow.css("left");
		currentPptPos._top = $pptWindow.css("top");

		$pptWindow.css({
			"width" : $(window).width(),
			"height" :  $(window).height() + 121,
			"left" : scrollLeftOffset,
			"top" : scrollTopOffset-121,
			"z-index" : 999
		});
		$pptWindow.addClass("ppt-fullscreen");
		$pptWindow.resizable("disable");
	}else if(type == 0){
		$("html").css("overflow","auto");
		$pptWindow.css({
			"width" : currentPptPos._width,
			"height" : currentPptPos._height,
			"left" : currentPptPos._left,
			"top" : currentPptPos._top,
			"z-index" : "auto"
		});
		$pptWindow.removeClass("ppt-fullscreen");
		$pptWindow.resizable("enable");
	}
	webplayer.pptInnerSize();
}

/**
* 窗口最小化及还原动画
* 参数from为窗口的id或class
* 参数to为底部快捷按钮的id或class
*/
var WidgetMinimize = function(from,to){
	if(!(this instanceof window.WidgetMinimize)){
		return new window.WidgetMinimize(from,to);
	}
	this.widget = $(from);
	this.cloneDiv = $('<div style="background-color:#fff; position:absolute; z-index:9999; overflow:hidden; opacity:0.9; filter:alpha(opacity=90);"></div>');
	this.port = $(to);
	this.minBtn = $(from).find(".close-icon");
	this.restBtn = $(to).children("a");	
	this.lastPos = {};
	this.portPos = {};
	
}
WidgetMinimize.prototype.minimize = function(){
	var that = this;
	var animateFinish = true;
	var getPortPos = function (){
		that.portPos = {
			"left" : that.port.offset().left + 15,
			"top" : that.port.offset().top + 55
		}
	}
	var getLastPos = function (){
		that.lastPos = {
			"width" : that.widget.css("width"),
			"height" : that.widget.css("height"),
			"left" : that.widget.css("left"),
			"top" : that.widget.css("top")
		}
	}
	var miniFn = function(){
		animateFinish = false;
		getLastPos();
		getPortPos();
		that.cloneDiv.css({
			"width" : that.widget.css("width"),
			"height" : that.widget.css("height"),
			"left" : that.widget.css("left"),
			"top" : that.widget.css("top")
		});
		
		$(".main").append(that.cloneDiv);
		that.widget.css({"visibility" : "hidden"});
		that.cloneDiv.animate({
			"width" : 20,
			"height" : 0,
			"left" : that.portPos.left,
			"top" : that.portPos.top
		},500,function(){
			that.cloneDiv.remove();
			animateFinish = true;
		});
	}
	var restFn = function(){
		animateFinish = false;	
		getLastPos();
		getPortPos();
		that.cloneDiv.css({
			"width" : 55,
			"height" : 0,
			"left" : that.portPos.left,
			"top" : that.portPos.top
		});
		
		$(".main").append(that.cloneDiv);
		that.cloneDiv.animate({
			"width" : that.lastPos.width,
			"height" : that.lastPos.height,
			"left" : that.lastPos.left,
			"top" : that.lastPos.top
		},500,function(){
			that.cloneDiv.remove();	
			that.widget.css({"visibility" : "visible"});
			animateFinish = true;
		});
	}
	
	that.minBtn.bind("click",miniFn);
	that.restBtn.bind("click",function(){	
		if(that.widget.css("visibility") == "hidden" && animateFinish){
			restFn();
		}else if(that.widget.css("visibility") == "visible" && animateFinish ){
			miniFn();
		}
		
	});
}


/**
 * Cookie plugin
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

//获取窗口最后的大小和位置等CSS
function getWidgetLastCss(){

	var widgetLastCss = {
		pptCss : {
			"visibility" : $(".ppt-container").css("visibility"),
			"width" : $(".ppt-container").css("width"),
			"height" : $(".ppt-container").css("height"),
			"z-index" : $(".ppt-container").css("z-index")
		},
		pptAnim : {
			"left" : $(".ppt-container").css("left"),
			"top" : $(".ppt-container").css("top")
		},
		pptIsMain : $(".ppt-container").hasClass("ppt-is-main"),
		qaCss : {
			"visibility" : $(".qa-container").css("visibility"),
			"z-index" : $(".qa-container").css("z-index")
		},
		qaAnim : {
			"width" : $(".qa-container").css("width"),
			"height" : $(".qa-container").css("height"),
			"left" : $(".qa-container").css("left"),
			"top" : $(".qa-container").css("top")
		},
		videoCss : {
			"visibility" : $(".video-container").css("visibility"),
			"width" : $(".video-container").css("width"),
			"height" : $(".video-container").css("height"),
			"z-index" : $(".video-container").css("z-index")
		},
		videoAnim : {
			"left" : $(".video-container").css("left"),
			"top" : $(".video-container").css("top")
		},
		chatCss : {
			"visibility" : $(".chat-container").css("visibility"),
			"z-index" : $(".chat-container").css("z-index")
		},
		chatAnim : {
			"width" : $(".chat-container").css("width"),
			"height" : $(".chat-container").css("height"),
			"left" : $(".chat-container").css("left"),
			"top" : $(".chat-container").css("top")
		},
		outlineCss : {
			"visibility" : $(".outline-container").css("visibility"),
			"z-index" : $(".outline-container").css("z-index")
		},
		outlineAnim : {
			"width" : $(".outline-container").css("width"),
			"height" : $(".outline-container").css("height"),
			"left" : $(".outline-container").css("left"),
			"top" : $(".outline-container").css("top")
		},
		textLiveCss : {
			"left" : $(".text-live-container").css("left"),
			"top" : $(".text-live-container").css("top"),
			"z-index" : $(".text-live-container").css("z-index")
		}
	}
	
	//对象转为字符串	
	var strWidgetLastCss = JSON.stringify(widgetLastCss);
	return strWidgetLastCss;
}

//关闭浏览器或刷新页面保存cookie
if(navigator.userAgent.indexOf("Firefox")>0){
	window.onbeforeunload=function (){
		setCookie();
	}
}else{
	window.onunload=function (){
		setCookie();
	}  
}